package com.wt.service.service.impl;

import com.wt.dao.demo.mapper.UserLoginMapper;
import com.wt.dao.demo.mapper.UserMapper;
import com.wt.dao.demo.model.User;
import com.wt.dao.demo.model.UserListModel;
import com.wt.dao.demo.model.UserLogin;
import com.wt.service.exception.WTException;
import com.wt.service.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserLoginMapper userLoginMapper;

    private final static Logger LOGGER = Logger.getLogger(UserServiceImpl.class);

    public void addUser(User user, UserLogin userLogin) throws Exception
    {

        //判断用户是否存在
        User resultUser = selectByUser(user);
        if(null != resultUser)
        {
            LOGGER.error("用户已存在！");
            throw new WTException("用户已存在！");
        }

        UserLogin resultUserLogin = selectUserLoginByUserName(userLogin.getUserName());
        if(null != resultUserLogin)
        {
            LOGGER.error("该用户名已被使用！");
            throw new WTException("该用户名已被使用！");
        }

        //新增用户
        Integer userId = addUser(user);

        //新增用户名密码
        userLogin.setUserId(userId);
        addUserLogin(userLogin);
    }

    /**
     * 新增用户
     * @param user
     * @throws Exception
     */
    public Integer addUser(User user) throws Exception {
        userMapper.insertSelective(user);
        Integer userId = user.getUserId();
        return userId;
    }

    /**
     * 查询用户
     * @param user
     * @return
     * @throws Exception
     */
    public User selectByUser(User user) throws Exception {
        User resultUser = userMapper.selectByUser(user);
        return resultUser;
    }

    /**
     * 新增用户名密码
     * @param userLogin
     * @throws Exception
     */
    public void addUserLogin(UserLogin userLogin) throws Exception {
        userLoginMapper.insertSelective(userLogin);
    }

    /**
     * 根据用户名查询
     * @param userName
     * @return
     * @throws Exception
     */
    public UserLogin selectUserLoginByUserName(String userName) throws Exception {
        UserLogin userLogin = userLoginMapper.selectUserLoginByUserName(userName);
        return userLogin;
    }

    public UserLogin selectUserLoginByUserNameAndPassWord(String userName, String passWord) throws Exception {
        UserLogin userLogin = new UserLogin();
        userLogin.setUserName(userName);
        userLogin.setPassword(passWord);
        UserLogin resultUserLogin = userLoginMapper.selectUserLoginByUserNameAndPassWord(userLogin);
        return resultUserLogin;
    }

    public User selectByUserId(Integer userId) throws Exception {
        User user = userMapper.selectByPrimaryKey(userId);
        if(null == user)
        {
            LOGGER.error("用户不存在！userId="+userId);
            throw new WTException("用户不存在！userId="+userId);
        }
        return user;
    }

    public  List<UserListModel> getUserList() throws Exception {
        List<UserListModel> userList = userMapper.getUserList();

        return userList;
    }
}
